package com.brayan.webapp.repository.usermgmt;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.brayan.webapp.model.usermgmt.Visit;

@Stateless
public class VisitRepository {
	
	@PersistenceContext
	private EntityManager em;
	
	@Asynchronous
	public void save(Visit visit){
		
		Visit existingVisit = findVisit(visit);
		if(existingVisit == null)
			em.persist(visit);
		else
			em.merge(visit);
	}
	
	
	public Visit findVisit(Visit visit){
		if(visit == null || visit.getId() == null || visit.getId() == 0)
			return null;
		return em.find(Visit.class, visit.getId());
	}
}
